﻿/** 
 * Description:商品评论DAO
 * Date:2011-7-16
 * Author:Along	
*/
package com.community.shopping.dao;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.community.shopping.domain.GoodsComment;
import com.community.shopping.service.logic.Page;

public class GoodsCommentDao extends HibernateDaoSupport{
	private Log log = LogFactory.getLog(GoodsBrandDao.class);  
	

	public void save(GoodsComment goodsComment){
		getHibernateTemplate().saveOrUpdate(goodsComment);	
		log.debug("-------保存一行goodsComment-------");
	}

	public void delete(GoodsComment goodsComment){		  
		getHibernateTemplate().delete(goodsComment);
		log.debug("---------删除一行goodsComment-------");
	}


	public List<?> getGoodsComment() {  		
	 return getHibernateTemplate().find(" from GoodsComment"); 
	 }

	public List<?> getGoodsComment(String sql){
		String finalsql="from GoodsComment where "+sql;
		return getHibernateTemplate().find(finalsql);
	}  
	
	public int getTotalCount(Integer goodsid)
	{
		Query   query   = this.getSession().createQuery("select count(*) from GoodsComment where goods.id="+goodsid+" and status=1 "); 
		int i=Integer.parseInt(query.list().get(0).toString());
		return i;
	}
	
	public float getGoodsAvgScore(Integer goodsid){
		Query   query   = this.getSession().createQuery("select avg(score) from GoodsComment where goods.id="+goodsid+" and status=1 "); 
		Float avg=Float.parseFloat(query.list().get(0).toString());
		return avg;
	} 
	
	
	public List<?> getGoodsComment(String sql,Page page){
		String finalsql=" from GoodsComment where "+sql;
		Query   query   = this.getSession().createQuery(finalsql+" order by createTime desc"); 
		query.setFirstResult(page.getPageStartRow()>0?page.getPageStartRow()-1:0); 
		query.setMaxResults(page.getPageSize()); 
		if(page.getTotalRowsAmount()==0)
		{
		 page.setTotalRowsAmount(getTotalCount(finalsql));
		}
	    List   stu   =   query.list(); 
		return stu;
	} 
	
	public int getTotalCount(String sql)
	{
		Query   query   = this.getSession().createQuery("select count(*) "+sql); 
		int i=Integer.parseInt(query.list().get(0).toString());
		return i;
	}
	}
